"""
Restful API 模块：股票信息
"""

from flask import (jsonify, request)

from app.web import manage
from app.web.controller import token_required
from app.apiservice.stockinfo import StockInfoApiService


@manage.route('/api/stock', methods=['POST'])
@token_required
def save_stock():
    """
    保存股票信息
    :return:
    """

    code = request.values.get('code', None)
    name = request.values.get('name', None)
    result = StockInfoApiService.save(code=code, name=name)
    return jsonify(result)


@manage.route('/api/stock', methods=['DELETE'])
@token_required
def remove_stock():
    """
    删除股票信息（逻辑删除）
    :return:
    """

    id_ = request.values.get('id', 0)
    result = StockInfoApiService.remove_by_id(id_=int(id_))
    return jsonify(result)


@manage.route('/api/stock', methods=['PUT'])
@token_required
def update_stock():
    """
    修改股票信息
    :return:
    """

    id_ = request.values.get('id', 0)
    name = request.values.get('name', None)
    status = request.values.get('status', 0)
    result = StockInfoApiService.update(id_=int(id_), name=name, status=int(status))
    return jsonify(result)


@manage.route('/api/stock', methods=['GET'])
@token_required
def find_stock():
    """
    查看股票信息
    :return:
    """

    id_ = request.values.get('id', 0)
    result = StockInfoApiService.find_by_id(id_=int(id_))
    return jsonify(result)


@manage.route('/api/stock/page', methods=['GET'])
@token_required
def page_stock():
    """
    分页查询股票列表信息
    :return:
    """

    # 模糊查询
    search_item = request.values.get('searchItem', None)
    code = request.values.get('code', None)
    name = request.values.get('name', None)
    type_ = request.values.get('type', None)
    status = request.values.get('status', None)
    page = request.values.get('page', 1)
    page_size = request.values.get('page_size', 10)
    result = StockInfoApiService.page_by_param(page=int(page), page_size=int(page_size), code=code, name=name,
                                               search_item=search_item, type_=type_, status=status)
    return jsonify(result)
